home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 3 / Cream of the Crop 3.iso / comm / wnos5src.zip / NETUSER.C < prev    next >
C/C++ Source or Header  |  1993-10-03  |  3KB  |  148 lines

  1. /* Miscellaneous format conversion subroutines */
  2. #include <ctype.h>
  3. #include <stdio.h>
  4. #include "global.h"
  5. #include "netuser.h"
  6. #include "socket.h"
  7. #include "domain.h"
  8.  
  9. /* Convert Internet address in ascii dotted-decimal format (44.0.0.1) to
  10.  * binary IP address
  11.  */
  12. int32
  13. aton(char *s)
  14. {
  15.     int i;
  16.     int32 n = 0;
  17.  
  18.     if(s == NULLCHAR)
  19.         return 0;
  20.  
  21.     for(i = 24; i >= 0; i -= 8) {
  22.         /* Skip any leading stuff (e.g., spaces, '[') */
  23.         while(*s != '\0' && !isdigit(*s))
  24.             s++;
  25.         if(*s == '\0')
  26.             break;
  27.         n |= (int32)atoi(s) << i;
  28.  
  29.         if((s = strchr(s,'.')) == NULLCHAR)
  30.             break;
  31.         s++;
  32.     }
  33.     return n;
  34. }
  35.  
  36. /* Convert an internet address (in host byte order) to a dotted decimal ascii
  37.  * string, e.g., 255.255.255.255\0
  38.  */
  39. char *
  40. inet_ntoa(int32 a)
  41. {
  42.     static char buf[20], *name;
  43.  
  44.     if(DTranslate && (name = resolve_a(a,!DVerbose)) != NULLCHAR) {
  45.         sprintf(buf,"%.16s",name);
  46.     } else {
  47.         sprintf(buf,"%u.%u.%u.%u",
  48.             hibyte(hiword(a)),lobyte(hiword(a)),
  49.             hibyte(loword(a)),lobyte(loword(a)) );
  50.     }
  51.     return buf;
  52. }
  53.  
  54. /* Convert hex-ascii string to long integer */
  55. long
  56. htol(char *s)
  57. {
  58.     long ret = 0;
  59.     char c;
  60.  
  61.     while((c = *s++) != '\0'){
  62.         c &= 0x7f;
  63.         if(c == 'x')
  64.             continue;    /* Ignore 'x', e.g., '0x' prefixes */
  65.         if(c >= '0' && c <= '9')
  66.             ret = ret * 16 + (c - '0');
  67.         else if(c >= 'a' && c <= 'f')
  68.             ret = ret * 16 + (10 + c - 'a');
  69.         else if(c >= 'A' && c <= 'F')
  70.             ret = ret * 16 + (10 + c - 'A');
  71.         else
  72.             break;
  73.     }
  74.     return ret;
  75. }
  76.  
  77. /* Return character string corresponding to a TCP well-known port,
  78.  * or the decimal number if unknown.
  79.  */
  80. char *
  81. tcp_port(int16 n)
  82. {
  83.     switch(n){
  84.     /* tcp ports */
  85.     case IPPORT_ECHO:
  86.         return "echo";
  87.     case IPPORT_DISCARD:
  88.         return "discard";
  89.     case IPPORT_FTPD:
  90.         return "ftp_data";
  91.     case IPPORT_FTP:
  92.         return "ftp";
  93.     case IPPORT_TELNET:
  94.         return "telnet";
  95.     case IPPORT_SMTP:
  96.         return "smtp";
  97.     case IPPORT_FINGER:
  98.         return "finger";
  99.     case IPPORT_TTYLINK:
  100.         return "chat";
  101.     case IPPORT_POP:
  102.         return "pop2";
  103.     case IPPORT_NNTP:
  104.         return "nntp";
  105.     case IPPORT_CONVERS:
  106.         return "convers";
  107.     case IPPORT_XCONVERS:
  108.         return "xconvers";
  109.  
  110.     /* udp ports */
  111.     case IPPORT_TIME:
  112.         return "time";
  113.     case IPPORT_DOMAIN:
  114.         return "domain";
  115.  
  116.     /* self-defined ports */
  117.     case 9980:
  118.         return "bbs";
  119.     case 9981:
  120.         return "node";
  121.     case 9982:
  122.         return "sys";
  123.     case 9983:
  124.         return "wnos";
  125.     case 9984:
  126.         return "modem";
  127.     case 9985:
  128.         return "event";
  129.     default:
  130.         return "";
  131.     }
  132. }
  133.  
  134. char *
  135. pinet(struct socket *s)
  136. {
  137.     static char buf[40];
  138.     char *cp;
  139.  
  140.     sprintf(buf,"%s:%s",inet_ntoa(s->address),tcp_port(s->port));
  141.  
  142.     if(*(cp = strchr(buf,':') + 1) < 'a')
  143.         sprintf(cp,"%u\0",s->port);
  144.  
  145.     return buf;
  146. }
  147.  
  148.